home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / getopt / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-29  |  1.3 KB  |  63 lines

  1. /*
  2. **  GETOPT PROGRAM AND LIBRARY ROUTINE
  3. **
  4. **  I wrote main() and AT&T wrote getopt() and we both put our efforts into
  5. **  the public domain via mod.sources.
  6. **    Rich $alz
  7. **    Mirror Systems
  8. **    (mirror!rs, rs@mirror.TMC.COM)
  9. **    August 10, 1986
  10. */
  11.  
  12. /* $Header: /sprite/src/cmds/getopt/RCS/getopt.c,v 1.2 90/10/29 13:19:34 kupfer Exp $ */
  13.  
  14. #include <stdio.h>
  15.  
  16.  
  17. #ifndef INDEX
  18. #define INDEX index
  19. #endif
  20.  
  21.  
  22. extern char    *INDEX();
  23. extern int     optind;
  24. extern char    *optarg;
  25.  
  26.  
  27. main(ac, av)
  28.     register int     ac;
  29.     register char     *av[];
  30. {
  31.     register char     *flags;
  32.     register int     c;
  33.  
  34.     /* Check usage. */
  35.     if (ac < 2) {
  36.     fprintf(stderr, "usage: %s flag-specification arg-list\n", av[0]);
  37.     exit(2);
  38.     }
  39.  
  40.     /* Play games; remember the flags (first argument), then splice
  41.        them out so it looks like a "standard" command vector. */
  42.     flags = av[1];
  43.     av[1] = av[0];
  44.     av++;
  45.     ac--;
  46.  
  47.     /* Print flags. */
  48.     while ((c = getopt(ac, av, flags)) != EOF) {
  49.     if (c == '?')
  50.         exit(1);
  51.     /* We assume that shells collapse multiple spaces in `` expansion. */
  52.     printf("-%c %s ", c, INDEX(flags, c)[1] == ':' ? optarg : "");
  53.     }
  54.  
  55.     /* End of flags; print rest of options. */
  56.     printf("-- ");
  57.     for (av += optind; *av; av++)
  58.     printf("%s ", *av);
  59.  
  60.     printf("\n");
  61.     exit(0);
  62. }
  63.